{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "if-branch-title",
      "metadata": {},
      "source": ["# Python if分支详解"]
    },
    {
      "cell_type": "markdown",
      "id": "if-intro",
      "metadata": {},
      "source": ["if分支是Python中最基础的条件控制结构，用于根据布尔表达式的结果决定是否执行特定代码块。其核心语法为：", "```python", "if condition1:", "    # 条件1满足时执行的代码", "elif condition2:", "    # 条件1不满足但条件2满足时执行的代码", "else:", "    # 所有条件都不满足时执行的代码", "```", "其中：", "- `condition` 是布尔表达式（结果为True/False）", "- `elif` 和 `else` 为可选分支（可省略）", "- 代码块通过缩进（通常4个空格）标识作用域"]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "if-basic-example",
      "metadata": {},
      "outputs": [],
      "source": ["# 基础if示例：判断数值正负", "num = 15", "if num > 0:", "    print(f'{num} 是正数')  # 条件满足时执行（num=15>0）", "elif num == 0:", "    print(f'{num} 是零')", "else:", "    print(f'{num} 是负数')"]
    },
    {
      "cell_type": "markdown",
      "id": "elif-usage",
      "metadata": {},
      "source": ["### 多条件判断（elif）", "`elif` 用于在第一个条件不满足时检查后续条件，可连续使用多个`elif`实现多分支判断。"]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "elif-example",
      "metadata": {},
      "outputs": [],
      "source": ["# 示例：根据分数判断等级（百分制）", "score = 85", "if score >= 90:", "    grade = 'A'", "elif score >= 80:", "    grade = 'B'  # 80≤score<90时执行", "elif score >= 70:", "    grade = 'C'", "else:", "    grade = 'D'", "print(f'分数{score}对应的等级：{grade}')"]
    },
    {
      "cell_type": "markdown",
      "id": "nested-if",
      "metadata": {},
      "source": ["### 嵌套if语句", "if分支内部可嵌套其他if语句，用于处理更复杂的条件逻辑。需严格注意缩进层级。"]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "nested-example",
      "metadata": {},
      "outputs": [],
      "source": ["# 示例：判断是否为偶数且大于10", "num = 12", "if num % 2 == 0:  # 先判断是否为偶数", "    if num > 10:  # 嵌套判断是否大于10（缩进更深一层）", "        print(f'{num} 是大于10的偶数')", "    else:", "        print(f'{num} 是不大于10的偶数')", "else:", "    print(f'{num} 是奇数')"]
    },
    {
      "cell_type": "markdown",
      "id": "boolean-rules",
      "metadata": {},
      "source": ["### 布尔表达式规则", "Python中除显式的True/False外，以下值会被视为False：", "- 空序列（如空字符串、空列表、空元组）", "- 数值0（包括0, 0.0, 0j）", "- None", "- 空字典、空集合", "逻辑运算符可组合多个条件：", "- `and`：全真为真，一假则假", "- `or`：全假为假，一真则真", "- `not`：取反"]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "boolean-example",
      "metadata": {},
      "outputs": [],
      "source": ["# 示例：布尔表达式测试", "user_input = ''  # 空字符串（视为False）", "age = 16", "if user_input and age >= 18:  # 逻辑与（需同时满足）", "    print('有效输入且成年')", "elif not user_input or age < 18:  # 逻辑或（满足其一即可）", "    print('输入为空或未成年')  # 此分支会被触发（user_input为空）"]
    },
    {
      "cell_type": "markdown",
      "id": "key-notes",
      "metadata": {},
      "source": ["## 注意事项", "1. 所有条件分支的冒号（`:`）不可省略", "2. 缩进必须一致（推荐4空格），否则会引发IndentationError", "3. `else` 必须放在最后，且不能单独使用", "4. 复杂条件建议用括号明确优先级（如 `(a > 0) and (b < 5)`）"]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "ml",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}